package com.demo.service;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.demo.model.Blog;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;

@Service
public class BlogService  {

	private Logger logger = Logger.getLogger(getClass());

	
	public Blog findBlogById(Integer id) {
		logger.debug("id: " + id);
		return Blog.dao.findById(id);
	}

	
	public boolean deleteBlogById(Integer id) {
		return Blog.dao.deleteById(id);
	}

	
	public boolean createOrUpdateBlog(Blog blog) {
		Integer id = blog.getInt("id");
		logger.debug("id: " + id);

		Date now = new Date();
		if (id == null || id == 0) {
			blog.set("createTime", now);
			blog.set("updateTime", now);
			return blog.save();
		} else {
			blog.set("updateTime", now);
			return blog.update();
		}
	}

	
	public Page<Blog> pageQueryBlog(Integer pageNumber, Integer pageSize) {
		return Blog.dao.paginate(pageNumber, pageSize);
	}
}
